package com.hhu.wangzb.server.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hhu.wangzb.common.entity.filter.EntityFilter;
import com.hhu.wangzb.common.entity.UserEntity;
import com.hhu.wangzb.server.service.UserService;
import com.hhu.wangzb.common.utils.PageUtils;
import com.hhu.wangzb.common.utils.R;



/**
 * 
 *
 * @author wangzb
 * @email 494199942@qq.com
 * @date 2021-07-03 17:37:58
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private EntityFilter entityFilter;

    /**
     * 获取全部数据（分页数据，排序数据...）
     */
    @GetMapping("/users")
    public R list(@RequestParam Map<String, Object> params, String filter) {
        PageUtils page = userService.queryPage(params);

        page.setList((List<?>) entityFilter.filterList(page.getList(), filter));

        return R.ok().put("page", page);
    }

    /**
     * 当前用户信息
     */
    @GetMapping("/user/current")
    public R authenticatedUserInfo(@RequestHeader("authentication") String authentication) {

        return R.ok().put("data", userService.queryAuthenticated(authentication));
    }


    /**
     * 获取某条数据
     */
    @GetMapping("/user/{id}")
    public R info(@PathVariable("id") Integer id, String filter) {
		UserEntity user = userService.getById(id);

        Object o = entityFilter.filterEntity(user, filter);

        return R.ok().put("user", o);
    }

    /**
     * 创建
     */
    @PutMapping("/user")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/user")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/user")
    public R delete(Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
